package com.yycwxt.modules.admin.shopcompare;

import java.util.Map;

public class ShopCompareProvider {

    public String shopcomsql(Map<String, Object> map){
        String [] shops = (String[]) map.get("shop");
        String tem = """
                select 
                left(subject_name,locate('-',subject_name)-1) '科目总名',
                subject_name '科目名称',
                count(case when 
                """;
        String tem1 = "shop_name = '%s' ";
        String tem2 = " then 1 end) '累计执行', " ;
        String tem3 = """
               count(case when shop_name = '%s' then 1 end) '%s',
               """ ;
        String tem4 = """
               count(case when shop_name = '%s' then 1 end) '%s'
               """ ;
        String a = " or ";
        String end = """
                from yycwxt_test.core_flows
                where subject_name is not null
                group by  科目总名, 科目名称
                """;
        StringBuilder sql = new StringBuilder();
        sql.append(tem);
        for (int i = 0; i < shops.length; i++) {
            sql.append(String.format(tem1, shops[i]));
            if (i<shops.length-1){
                sql.append(a);
            }
        }
        sql.append(tem2);
        for (int i = 0; i < shops.length; i++) {
            sql.append(String.format(tem3, shops[i],shops[i]));
            if (i == shops.length-1){
                sql.append(String.format(tem4, shops[i], shops[i]));
            }
        }
        sql.append(end);
        return sql.toString();
    }
}
